package com.sxt.sys.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxt.sys.constast.SYSConstast;
import com.sxt.sys.domain.Menu;
import com.sxt.sys.domain.Menus;
import com.sxt.sys.service.MenuService;
import com.sxt.sys.service.RoleService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.TreeNode;
import com.sxt.sys.vo.MenuVo;
import com.sxt.sys.vo.RoleVo;


@Controller
@RequestMapping("role")
public class RoleController {
	      @Autowired
       private RoleService roleService;
	      @Autowired
	      private MenuService menuService;
		
		/**
		 * 跳转到客户管理的页面
		 */
		@RequestMapping("toRoleManager")
		public String toRoleManager(){
			return "system/roleManager";
		}
		/**
		 * 查询
		 */
		@RequestMapping("loadAllRole")
		@ResponseBody
		public DataGridView loadAllRole(RoleVo roleVo){
			/*DataGridView dg = this.roleService.queryAllRole(roleVo);
			System.out.println(dg.getTotal());*/
			return  this.roleService.queryAllRole(roleVo);
		}
		@RequestMapping("addRole")
		@ResponseBody
		public Map<String,Object> addRole(RoleVo roleVo){
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.roleService.addRole(roleVo);
				 map.put("msg", "添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "添加失败");
			}
			return  map;
		}
		@RequestMapping("updateRole")
		@ResponseBody
		public Map<String,Object> updateRole(RoleVo roleVo){
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.roleService.updateRole(roleVo);
				 map.put("msg", "修改成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "修改失败");
			}
			return  map;
		}
		
		//删除
		/*@RequestMapping("deleteRole")
		@ResponseBody
		public Map<String,Object> deleteRole(RoleVo roleVo){
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.roleService.deleteRole(roleVo);
				 map.put("msg", "添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "添加失败");
			}
			return  map;
		}*/
		//删除
		@RequestMapping("deleteRole")
		@ResponseBody
		public void deleteRole(RoleVo roleVo){
			
				 this.roleService.deleteRole(roleVo);
		}
		
		//加载分配菜单的“多选树”
		@RequestMapping("loadRoleSelectMenusTree")
		@ResponseBody
		public List<TreeNode> loadRoleSelectMenusTree(RoleVo roleVo){
			Integer roleid=roleVo.getRoleid();  //获取选中角色的id
			//1.查询所有可用菜单
			Menu menuVo=new MenuVo();
			menuVo.setAvailable(SYSConstast.AVAILABLE_TRUE);
			List<Menu> allMenus=this.menuService.queryAllMenusForList(menuVo);
			//2.根据角色ID查询当前角色拥有的菜单
			List<Menu> currRoleMenus=this.menuService.queryMenusByRoleIdForList(roleid,SYSConstast.AVAILABLE_TRUE);
			List<TreeNode> treeNodes=new ArrayList<>();
			for(Menu menu:allMenus){
				Boolean checked=false;
				for(Menu m2 : currRoleMenus){
					if(menu.getId()==m2.getId()){
						checked=true;
						break;
					}
				}
				Integer id=menu.getId();
				Integer pid=menu.getPid();
				String name=menu.getName();
				Boolean isParent=menu.getParent()==SYSConstast.MENU_PARENT_TRUE?true:false;
				Boolean open=menu.getOpen()==SYSConstast.MENU_OPEN_TRUE?true:false;
				String icon=menu.getIcon();
			
				treeNodes.add(new TreeNode(id, pid, name, isParent, open, icon, checked));
			}
			return treeNodes;
		}
		
		//保存角色和菜单之间的关系
		@RequestMapping("saveRoleMenus")
		@ResponseBody
		public Map<String,Object> saveRoleMenus(RoleVo roleVo){
			Map<String,Object> map=new HashMap<>();
			try {
				this.roleService.saveRoleMenus(roleVo);
				map.put("msg", "分配成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("msg", "分配失败");
			}
			return map;
		}
	}
